﻿/**************************************************************************
创建时间:	2020/5/14 15:08:36    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using ASmile.Com;
using ASmile.Com.Search;
using ASmile.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ASmile.Win.ADSearch
{
    public partial class FrmADSearch : FrmBaseModal
    {
        public FrmADSearch()
        {
            InitializeComponent();
        }
        public FrmADSearch(ArgPageData arg, string dgvName = "dgv")
            : this(arg.AdSearchList, dgvName)
        { }

        public FrmADSearch(List<AdSearchDetail> searchList, string dgvName = "dgv")
        {
            InitializeComponent();
            DgvName = dgvName;
            SearchDetail = searchList;
            SearchDetail.Clear();
        }


        List<AdSearchDetail> SearchDetail { get; }

        //ISqlBuilder _ADBuiler;
        string DgvName { get; }
        string FormName { get; set; }
        Sys_ADRecord CurrRecord { get; set; }
        List<Sys_GridColumn> ColumnList { get; set; }

        ISearchBLL BLLObj { get; } = ObjectFactory.GetObject<ISearchBLL>("SearchBLL");

        void ClearSearchDetail()
        {
            flowLayoutPanel1.Controls.Clear();
        }
        /// <summary>
        /// 载入搜索记录
        /// </summary>
        void LoadSearchDetail()
        {
            ClearSearchDetail();
            var SearchDetail = BLLObj.GetRecordDetail(new ArgMainId(CurrRecord.Id)).data;
            if (SearchDetail == null) return;
            for (int i = 0; i < SearchDetail.Count; i++)
            {
                U_ASRow u1 = new U_ASRow();
                if (i == 0)
                {
                    u1.IsFirstRow = true;
                }
                u1.InitiColumns(ColumnList);
                u1.SetDetail(SearchDetail[i]);
                flowLayoutPanel1.Controls.Add(u1);
            }
        }
        /// <summary>
        /// 获取搜索明细结果
        /// </summary>
        List<Sys_ADRecord_Detail> GetSearchList()
        {
            List<Sys_ADRecord_Detail> list = new List<Sys_ADRecord_Detail>();
            int orderNo = 0;
            foreach (U_ASRow item in flowLayoutPanel1.Controls)
            {
                if (item == null) continue;
                var sm = item.GetDetail();
                if (sm == null) continue;
                sm.OrderNo = orderNo++;
                list.Add(sm);
            }
            return list;
        }
        /// <summary>
        /// 模态打开高级搜索输入
        /// </summary>
        public bool ShowSearch(IFormContainer form)
        {
            //_BLL = new SearchBLL();
            FormName = form.ContainerName;
            ColumnList = BLLObj.GetColumns(new Com.GridView.ArgGetList { FormName = FormName, DgvName = DgvName }).data;
            CurrRecord = BLLObj.GetDefRecord(new ArgRecordSearch { FormName = FormName, DgvName = DgvName }).data;
            if (CurrRecord == null)
            {
                CurrRecord = new Sys_ADRecord();
                CurrRecord.FormName = FormName;
                CurrRecord.DgvName = DgvName;
                CurrRecord.Caption = "缺省";
                CurrRecord.CaptionCode = "qs";
            }
            else
            {
                LoadSearchDetail();
            }
            var ret = ShowDialog();
            return ret == DialogResult.OK;
        }

        private void FrmADSearch_Load(object sender, EventArgs e)
        { }

        private void tsBtnAddOneRow_Click(object sender, EventArgs e)
        {
            U_ASRow ua = new U_ASRow();
            ua.InitiColumns(ColumnList);
            if (flowLayoutPanel1.Controls.Count == 0)
            {
                ua.IsFirstRow = true;
            }
            flowLayoutPanel1.Controls.Add(ua);
        }

        private void tsBtnClear_Click(object sender, EventArgs e)
        {
            ClearSearchDetail();
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            List<Sys_ADRecord_Detail> detailList = GetSearchList();
            if (detailList == null || detailList.Count == 0)
            {
                ShowMessage("搜索明细为空");
                return;
            }
            FrmADSave frm = new FrmADSave();

            var saveModel = new Sys_ADRecord
            {
                DgvName = this.DgvName,
                FormName = this.FormName
            };
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.SaveRecord(new ArgSaveRecord { Record = m as Sys_ADRecord, Details = detailList }),
                SaveModel = saveModel
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnConfigLoad_Click(object sender, EventArgs e)
        {
            FrmADRecord frm = new FrmADRecord();
            frm.GetDataSource = (serVal) => BLLObj.GetRecordList(new ArgRecordSearch
            {
                FormName = FormName,
                SearchVal = serVal,
                DgvName = DgvName
            });
            frm.SaveRecord = BLLObj.UpdateRecord;
            frm.DelRecord = BLLObj.DelRecord;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                CurrRecord = frm.SelectedRecord;
                LoadSearchDetail();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var detailList = GetSearchList();
            BLLObj.SaveRecord(new ArgSaveRecord { Record = CurrRecord, Details = detailList }); //CurrRecord, detailList);
            foreach (var item in detailList)
            {
                SearchDetail.Add(new AdSearchDetail
                {
                    CompareSignStr = item.CompareSignStr,
                    ConnectorSignStr = item.ConnectorSignStr,
                    DataTypeStr = item.DataTypeStr,
                    FieldName = item.FieldName,
                    SearchValue = item.SearchValue
                });
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}